package com.fcscs.cservice.client.main.configuration.reservation;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.common.authority.CServiceAuthority;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.domain.IdNameModel;
import com.fcscs.core.domain.Property;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.CServiceCommonServiceController;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.common.CServiceCommonEnums.EditableCategoryEnum;
import com.fcscs.cservice.common.CServiceCommonEnums.EditableFieldEnum;
import com.fcscs.cservice.domain.FieldEditability;
import com.fcscs.cservice.domain.model.EditabilityConf;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.google.gwt.user.client.ui.Widget;

public class RCConfigPanel extends FcsPanel {
	private VerticalPanel vp;
	private CheckBox cbper;
	private CheckBox cb11;
	private CheckBox cb12;
	private CheckBox cb13;
	private CheckBox cb14;
	private CheckBox cb15;
	
	private CheckBox cbrev;
	private CheckBox cb21;
	private CheckBox cb22;
	private CheckBox cb23;
	private CheckBox cb24;
	private CheckBox cb25;
	private CheckBox cb26;
	private CheckBox cb27;
	
	private CheckBox cbcon;
	private CheckBox cb30;
	private CheckBox cb31;
	private CheckBox cb32;
	private CheckBox cb33;
	private CheckBox cb34;
	private CheckBox cb35;
	private CheckBox cb36;
	private CheckBox cb37;
	private CheckBox cb38;
	
	private CheckBox cbpay;
	private CheckBox cb41;
	private CheckBox cb42;
	
	private CheckBox cb51;
	private CheckBox cb52;
	
	private List<EditableCategoryEnum> categories;
	private List<FieldEditability> myFieldEditabilityList;
	
	private EasyClearCombobox<PropertyIdNameModelData> propCombox;
	private ListStore<PropertyIdNameModelData> propListStore;
	private List<IdNameModel<Property>> propGroupIdNames;
	
	public RCConfigPanel(String id) {
		super(id);
	}

	@Override
	public void init() {
		vp = new VerticalPanel();
		vp.setId("RCConfigPanel_vpPanel");
//		vp.setSpacing(5);
		myFieldEditabilityList = new ArrayList<FieldEditability>();
		
		LayoutContainer lc = new LayoutContainer();
		lc.setId("RCConfigPanel_lc");
		lc.setLayout(new ColumnLayout());
		
		Label propLabel = new Label(CoreConstants.CORE_CONSTANTS.property() + CoreConstants.REQURIED_FIELD_LABEL);
		propLabel.setId("RCConfigPanel_propLabel");
		
		propListStore = new ListStore<PropertyIdNameModelData>();
		propCombox = new EasyClearCombobox<PropertyIdNameModelData>();
		propCombox.setId("RCConfigPanel_propCombox");
//		propCombox.setFieldLabel(CoreConstants.CORE_CONSTANTS.property());
		propCombox.setWidth(200);
		propCombox.setDisplayField(CoreConstants.FULLTNAME);
		propCombox.setAllowBlank(false);
		propCombox.setForceSelection(true);
		propCombox.setTriggerAction(TriggerAction.ALL);
		propCombox.setStore(propListStore);
		propCombox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>(){
			@Override
			public void selectionChanged(
					SelectionChangedEvent<PropertyIdNameModelData> se) {
				CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
						propCombox.getValue().getEntity().getId(), 
						categories, new AsyncCallbackListener<EditabilityConf>(){
							@Override
							public void onLogin(String username) {
								CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
										SessionContext.getInstance().getPropertyId(), 
										categories,this);
							}
							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
										CoreConstants.CORE_CONSTANTS.systemError(), null);
							}
							@Override
							public void onSuccess(EditabilityConf result) {
								if(null != result){
									updateForm(result);
								}
							}
						});
			}
		});
		
		lc.add(propLabel, new ColumnData(70));
		lc.add(propCombox, new ColumnData(160));
		
		vp.add(lc);
		
		
		FormPanel panel1 = new FormPanel();
		panel1.setId("RCConfigPanel_panel1");
		panel1.setFrame(true);
		panel1.setHeading(CServiceConstants.CSERVICE_CONSTANTS.personal());
		panel1.setPadding(0);
		panel1.setSize(330, 210);
		panel1.setLabelAlign(LabelAlign.LEFT);
		panel1.setLabelSeparator("");
//		panel1.setLabelWidth(30);
		
		cbper =new CheckBox();
		cbper.setId("RCConfigPanel_cbper");
		cbper.setBoxLabel("<html><u>"+CServiceConstants.CSERVICE_CONSTANTS.personal() +"</u></html>");
		cbper.addListener(Events.Change, new Listener<BaseEvent>(){
			@Override
			public void handleEvent(BaseEvent be) {
				if(cbper.getValue()){
					cb11.setValue(true);
					cb12.setValue(true);
					cb13.setValue(true);
					cb14.setValue(true);
					cb15.setValue(true);
				}else{
					cb11.setValue(false);
					cb12.setValue(false);
					cb13.setValue(false);
					cb14.setValue(false);
					cb15.setValue(false);
				}
			}
		});

		cb11 =new CheckBox();
		cb11.setId("RCConfigPanel_cb11");
		cb11.setBoxLabel(CoreConstants.CORE_CONSTANTS.firstname());
		cb11.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb12 =new CheckBox();
		cb12.setId("RCConfigPanel_cb12");
		cb12.setBoxLabel(CoreConstants.CORE_CONSTANTS.lastname());
		cb12.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb13 =new CheckBox();
		cb13.setId("RCConfigPanel_cb13");
		cb13.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.title());
		cb13.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb14 =new CheckBox();
		cb14.setId("RCConfigPanel_cb14");
		cb14.setBoxLabel(CoreConstants.CORE_CONSTANTS.gender());
		cb14.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb15 =new CheckBox();
		cb15.setId("RCConfigPanel_cb15");
		cb15.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.nationality());
		cb15.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		
		panel1.add(cbper);
		panel1.add(cb11);
		panel1.add(cb12);
		panel1.add(cb13);
		panel1.add(cb14);
		panel1.add(cb15);
		
		FormPanel panel2 = new FormPanel();
		panel2.setId("RCConfigPanel_FormPanel");
		panel2.setFrame(true);
		panel2.setHeading(CServiceConstants.CSERVICE_CONSTANTS.reservation());
		panel2.setPadding(0);
		panel2.setSize(330, 210);
		panel2.setLabelAlign(LabelAlign.LEFT);
		panel2.setLabelSeparator("");
		
		cbrev =new CheckBox();
		cbrev.setId("RCConfigPanel_cbrev");
		cbrev.setBoxLabel("<html><u>" + CServiceConstants.CSERVICE_CONSTANTS.reservation() + "</u></html>");
		cbrev.addListener(Events.Change, new Listener<BaseEvent>(){
			@Override
			public void handleEvent(BaseEvent be) {
				if(cbrev.getValue()){
					cb21.setValue(true);
					cb22.setValue(true);
					cb23.setValue(true);
					cb24.setValue(true);
					cb25.setValue(true);
					cb26.setValue(true);
					cb27.setValue(true);
				}else{
					cb21.setValue(false);
					cb22.setValue(false);
					cb23.setValue(false);
					cb24.setValue(false);
					cb25.setValue(false);
					cb26.setValue(false);
					cb27.setValue(false);
				}
			}
		});

		cb21 =new CheckBox();
		cb21.setId("RCConfigPanel_cb21");
		cb21.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.noOfGuest());
		cb21.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb22 =new CheckBox();
		cb22.setId("RCConfigPanel_cb22");
		cb22.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.shareReservation());
		cb22.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb23 =new CheckBox();
		cb23.setId("RCConfigPanel_cb23");
		cb23.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.arrivalDate());
		cb23.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb24 =new CheckBox();
		cb24.setId("RCConfigPanel_cb24");
		cb24.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.departureDate());
		cb24.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb25 =new CheckBox();
		cb25.setId("RCConfigPanel_cb25");
		cb25.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.arrivalTime());
		cb25.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb26 =new CheckBox();
		cb26.setId("RCConfigPanel_cb26");
		cb26.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.departrueTime());
		cb26.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb27 =new CheckBox();
		cb27.setId("RCConfigPanel_cb27");
		cb27.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.expressCheckOut());
		cb27.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		
		panel2.add(cbrev);
		panel2.add(cb21);
		panel2.add(cb22);
		panel2.add(cb23);
		panel2.add(cb24);
		panel2.add(cb25);
		panel2.add(cb26);
		panel2.add(cb27);
		
		FormPanel panel3 = new FormPanel();
		panel3.setId("RCConfigPanel_panel3");
		panel3.setFrame(true);
		panel3.setHeading(CServiceConstants.CSERVICE_CONSTANTS.contact());
		panel3.setPadding(0);
		panel3.setSize(330, 240);
		panel3.setLabelAlign(LabelAlign.LEFT);
		panel3.setLabelSeparator("");
		
		cbcon =new CheckBox();
		cbcon.setId("RCConfigPanel_cbcon");
		cbcon.setBoxLabel("<html><u>" + CServiceConstants.CSERVICE_CONSTANTS.contact() + "</u></html>");
		cbcon.addListener(Events.Change, new Listener<BaseEvent>(){
			@Override
			public void handleEvent(BaseEvent be) {
				if(cbcon.getValue()){
					cb30.setValue(true);
					cb31.setValue(true);
					cb32.setValue(true);
					cb33.setValue(true);
					cb34.setValue(true);
					cb35.setValue(true);
					cb36.setValue(true);
					cb37.setValue(true);
					cb38.setValue(true);
				}else{
					cb30.setValue(false);
					cb31.setValue(false);
					cb32.setValue(false);
					cb33.setValue(false);
					cb34.setValue(false);
					cb35.setValue(false);
					cb36.setValue(false);
					cb37.setValue(false);
					cb38.setValue(false);
				}
			}
		});

		cb38 =new CheckBox();
		cb38.setId("RCConfigPanel_cb38");
		cb38.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.homeOrBusiness());
		cb38.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb30 =new CheckBox();
		cb30.setId("RCConfigPanel_cb30");
		cb30.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.address());
		cb30.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb31 =new CheckBox();
		cb31.setId("RCConfigPanel_cb31");
		cb31.setBoxLabel(CoreConstants.CORE_CONSTANTS.email());
		cb31.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb32 =new CheckBox();
		cb32.setId("RCConfigPanel_cb32");
		cb32.setBoxLabel(CoreConstants.CORE_CONSTANTS.mobile());
		cb32.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb33 =new CheckBox();
		cb33.setId("RCConfigPanel_cb33");
		cb33.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.city());
		cb33.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb34 =new CheckBox();
		cb34.setId("RCConfigPanel_cb34");
		cb34.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.state());
		cb34.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb35 =new CheckBox();
		cb35.setId("RCConfigPanel_cb35");
		cb35.setBoxLabel(CoreConstants.CORE_CONSTANTS.country());
		cb35.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb36 =new CheckBox();
		cb36.setId("RCConfigPanel_cb36");
		cb36.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.zip());
		cb36.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb37 =new CheckBox();
		cb37.setId("RCConfigPanel_cb37");
		cb37.setBoxLabel(CoreConstants.CORE_CONSTANTS.phone());
		cb37.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		
		panel3.add(cbcon);
		panel3.add(cb38);
		panel3.add(cb30);
		panel3.add(cb31);
		panel3.add(cb32);
		panel3.add(cb33);
		panel3.add(cb34);
		panel3.add(cb35);
		panel3.add(cb36);
		panel3.add(cb37);
		
		FormPanel panel4 = new FormPanel();
		panel4.setId("RCConfigPanel_panel4");
		panel4.setFrame(true);
		panel4.setHeading(CServiceConstants.CSERVICE_CONSTANTS.payment());
		panel4.setPadding(0);
		panel4.setSize(330, 240);
		panel4.setLabelAlign(LabelAlign.LEFT);
		panel4.setLabelSeparator("");
		
		cbpay =new CheckBox();
		cbpay.setId("RCConfigPanel_cbpay");
		cbpay.setBoxLabel("<html><u>" + CServiceConstants.CSERVICE_CONSTANTS.payment() + "</u></html>");
		cbpay.addListener(Events.Change, new Listener<BaseEvent>(){
			@Override
			public void handleEvent(BaseEvent be) {
				if(cbpay.getValue()){
					cb41.setValue(true);
					cb42.setValue(true);
				}else{
					cb41.setValue(false);
					cb42.setValue(false);
				}
			}
		});

		cb41 =new CheckBox();
		cb41.setId("RCConfigPanel_cb41");
		cb41.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentComment());
		cb41.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		cb42 =new CheckBox();
		cb42.setId("RCConfigPanel_cb42");
		cb42.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentMethod());
		cb42.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		
		panel4.add(cbpay);
		panel4.add(cb41);
		panel4.add(cb42);
		
		FormPanel panel5 = new FormPanel();
		panel5.setId("RCConfigPanel_panel5");
		panel5.setFrame(true);
		panel5.setHeading(CServiceConstants.CSERVICE_CONSTANTS.others());
		panel5.setPadding(0);
		panel5.setSize(330, 30);
		panel5.setLabelAlign(LabelAlign.LEFT);
		panel5.setLabelSeparator("");

		cb51 =new CheckBox();
		cb51.setId("RCConfigPanel_cb51");
		cb51.setBoxLabel(CServiceConstants.CSERVICE_CONSTANTS.memberShip());
		cb52 =new CheckBox();
		cb52.setId("RCConfigPanel_cb52");
		cb52.setBoxLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.remarks());
		
//		panel5.add(cb51);
		panel5.add(cb52);
		
		FormButton saveButton = new FormButton(CoreConstants.CORE_CONSTANTS.save());
		saveButton.setId("RCConfigPanel_saveButton");
		saveButton.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		saveButton.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "30px");
		saveButton.setSize(60, 30);
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
			@Override
			public void componentSelected(ButtonEvent ce) {
				CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
						propCombox.getValue().getEntity().getId(), 
						categories, new AsyncCallbackListener<EditabilityConf>(){
							@Override
							public void onLogin(String username) {
								CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
										SessionContext.getInstance().getPropertyId(), 
										categories,this);
							}
							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
										CoreConstants.CORE_CONSTANTS.systemError(), null);
							}
							@Override
							public void onSuccess(EditabilityConf result) {
								if(null != result){
									myFieldEditabilityList.clear();
									myFieldEditabilityList.addAll(result.getEditableFieldList(EditableCategoryEnum.PERSONAL));
									myFieldEditabilityList.addAll(result.getEditableFieldList(EditableCategoryEnum.RESERVATION));
									myFieldEditabilityList.addAll(result.getEditableFieldList(EditableCategoryEnum.CONTACT));
									myFieldEditabilityList.addAll(result.getEditableFieldList(EditableCategoryEnum.PAYMENT));
									
									populatEntity(myFieldEditabilityList);
									CServiceCommonServiceController.getINSTANCE().updateFieldEditability(
											myFieldEditabilityList, new AsyncCallbackListener<List<FieldEditability>>(){
												@Override
												public void onLogin(String username) {
													CServiceCommonServiceController.getINSTANCE().updateFieldEditability(
															myFieldEditabilityList,this);
												}

												@Override
												public void onFailure(Throwable caught) {
													MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
															CoreConstants.CORE_CONSTANTS.systemError(), null);
												}

												@Override
												public void onSuccess(List<FieldEditability> result) {
													if(null != result){
														CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
													}
												}
											});
								}
							}
						});
				
				
			}
		});
		
		FormButton clearButton = new FormButton(CoreConstants.CORE_CONSTANTS.clear());
		clearButton.setId("RCConfigPanel_clearButton");
		clearButton.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		clearButton.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "30px");
		clearButton.setSize(60, 30);
		clearButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
			@Override
			public void componentSelected(ButtonEvent ce) {
				cb51.clear();
				cb52.clear();
				cbpay.setValue(false);
				cbpay.fireEvent(Events.Change);
				cbcon.setValue(false);
				cbcon.fireEvent(Events.Change);
				cbrev.setValue(false);
				cbrev.fireEvent(Events.Change);
				cbper.setValue(false);
				cbper.fireEvent(Events.Change);
			}
		});

		HorizontalPanel hp1 = new HorizontalPanel();
		hp1.setId("RCConfigPanel_hp1");
		hp1.setSpacing(5);
		hp1.add(panel1);
		hp1.add(panel2);
		HorizontalPanel hp2 = new HorizontalPanel();
		hp2.setId("RCConfigPanel_hp2");
		hp2.setSpacing(5);
		hp2.add(panel3);
		hp2.add(panel4);
		HorizontalPanel hp3 = new HorizontalPanel();
		hp3.setId("RCConfigPanel_hp3");
		hp3.setSpacing(5);
		hp3.add(panel5);
		hp3.add(saveButton);
		hp3.add(clearButton);
		
		Label label = new Label(CServiceConstants.CSERVICE_CONSTANTS.updateToPMS() + CServiceConstants.FIELD_SEP);
		label.setId("RCConfigPanel_label");
		label.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
		label.setHeight(30);
		
//		vp.add(label);
		vp.add(hp1);
		vp.add(hp2);
		vp.add(hp3);
		
		add(vp);
		onShow();
	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return CServiceAuthority.cSERVICE_CONFIG;
	}

	@Override
	protected Widget getViewPanel() {
		return vp;
	}

	@Override
	public void onShow() {
		loadProperty();
		categories = new ArrayList<EditableCategoryEnum>();
		for(EditableCategoryEnum ca: EditableCategoryEnum.values()){
			categories.add(ca);
		}
		
		CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
			SessionContext.getInstance().getPropertyId(), 
			categories, new AsyncCallbackListener<EditabilityConf>(){
				@Override
				public void onLogin(String username) {
					CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
							SessionContext.getInstance().getPropertyId(), 
							categories,this);
				}
				@Override
				public void onFailure(Throwable caught) {
					MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
							CoreConstants.CORE_CONSTANTS.systemError(), null);
				}
				@Override
				public void onSuccess(EditabilityConf result) {
					if(null != result){
						updateForm(result);
					}
				}
			});
	}

	@Override
	public boolean onHide() {
		return true;
	}
	
	private void updateForm(EditabilityConf editabilityConf){
		List<FieldEditability> personalEditConfList= editabilityConf.getEditableFieldList(EditableCategoryEnum.PERSONAL);
		if (personalEditConfList != null && personalEditConfList.size() > 0) {
			myFieldEditabilityList.addAll(personalEditConfList);
			boolean isAll = true;
			for (FieldEditability fe : personalEditConfList) {
				if (fe.getFieldEnum().equals(EditableFieldEnum.FIRST_NAME)) {
					if(fe.getEditable()){
						cb11.setValue(true);
					}else{
						isAll = false;
						cb11.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.LAST_NAME)){
					if(fe.getEditable()){
						cb12.setValue(true);	
					}else{
						isAll = false;
						cb12.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.TITLE)){
					if(fe.getEditable()){
						cb13.setValue(true);	
					}else{
						isAll = false;
						cb13.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.GENDER)){
					if(fe.getEditable()){
						cb14.setValue(true);	
					}else{
						isAll = false;
						cb14.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.NATIONALITY)){
					if(fe.getEditable()){
						cb15.setValue(true);	
					}else{
						isAll = false;
						cb15.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.EMAIL)) {
					if(fe.getEditable()){
						cb31.setValue(true);
					}else{
						cb31.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.MOBILE)){
					if(fe.getEditable()){
						cb32.setValue(true);	
					}else{
						cb32.setValue(false);
					}
				}
			}
			if(isAll){
				cbper.setValue(true);
			}else{
				cbper.setValue(false);
			}
		}
		
		List<FieldEditability> resEditConfList= editabilityConf.getEditableFieldList(EditableCategoryEnum.RESERVATION);
		if (resEditConfList != null && resEditConfList.size() > 0) {
			myFieldEditabilityList.addAll(resEditConfList);
			boolean isAll = true;
			for (FieldEditability fe : resEditConfList){
				if (fe.getFieldEnum().equals(EditableFieldEnum.NO_OF_GUEST)) {
					if(fe.getEditable()){
						cb21.setValue(true);
					}else{
						isAll = false;
						cb21.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.SHARE_RESERVATION)){
					if(fe.getEditable()){
						cb22.setValue(true);	
					}else{
						isAll = false;
						cb22.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.ARRIVAL_DATE)){
					if(fe.getEditable()){
						cb23.setValue(true);	
					}else{
						isAll = false;
						cb23.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.DEPART_DATE)){
					if(fe.getEditable()){
						cb24.setValue(true);	
					}else{
						isAll = false;
						cb24.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.ARRIVAL_TIME)){
					if(fe.getEditable()){
						cb25.setValue(true);	
					}else{
						isAll = false;
						cb25.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.DEPART_TIME)){
					if(fe.getEditable()){
						cb26.setValue(true);	
					}else{
						isAll = false;
						cb26.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.EXPRESS_CHECK_OUT)){
					if(fe.getEditable()){
						cb27.setValue(true);	
					}else{
						isAll = false;
						cb27.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.RC_REMARKS)){
					if(fe.getEditable()){
						cb52.setValue(true);	
					}else{
						cb52.setValue(false);
					}
				}
			}
			if(isAll){
				cbrev.setValue(true);
			}else{
				cbrev.setValue(false);
			}
		}
		
		List<FieldEditability> contactEditConfList= editabilityConf.getEditableFieldList(EditableCategoryEnum.CONTACT);
		if (contactEditConfList != null && contactEditConfList.size() > 0) {
			myFieldEditabilityList.addAll(contactEditConfList);
			boolean isAll = true;
			for (FieldEditability fe : contactEditConfList){
				if(fe.getFieldEnum().equals(EditableFieldEnum.ADDRESS)) {
					if(fe.getEditable()){
						cb30.setValue(true);
					}else{
						cb30.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.CITY)){
					if(fe.getEditable()){
						cb33.setValue(true);	
					}else{
						isAll = false;
						cb33.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.STATE)){
					if(fe.getEditable()){
						cb34.setValue(true);	
					}else{
						isAll = false;
						cb34.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.COUNTRY)){
					if(fe.getEditable()){
						cb35.setValue(true);	
					}else{
						isAll = false;
						cb35.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.POSTAL)){
					if(fe.getEditable()){
						cb36.setValue(true);	
					}else{
						isAll = false;
						cb36.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.PHONE)){
					if(fe.getEditable()){
						cb37.setValue(true);	
					}else{
						isAll = false;
						cb37.setValue(false);
					}
				}
			}
			if(isAll){
				cbcon.setValue(true);
			}else{
				cbcon.setValue(false);
			}
		}
		
		List<FieldEditability> paymentEditConfList= editabilityConf.getEditableFieldList(EditableCategoryEnum.PAYMENT);
		if (paymentEditConfList != null && paymentEditConfList.size() > 0) {
			myFieldEditabilityList.addAll(paymentEditConfList);
			boolean isAll = true;
			for (FieldEditability fe : paymentEditConfList){
				if (fe.getFieldEnum().equals(EditableFieldEnum.PAYMENT_COMMENT)) {
					if(fe.getEditable()){
						cb41.setValue(true);
					}else{
						isAll = false;
						cb41.setValue(false);
					}
				}else if(fe.getFieldEnum().equals(EditableFieldEnum.PAYMENT_METHOD)){
					if(fe.getEditable()){
						cb42.setValue(true);	
					}else{
						isAll = false;
						cb42.setValue(false);
					}
				}
			}
			if(isAll){
				cbpay.setValue(true);
			}else{
				cbpay.setValue(false);
			}
		}
	}
	
	private void populatEntity(List<FieldEditability> myFieldEditabilityList){
		for(FieldEditability fe : myFieldEditabilityList){
			fe.setPropertyId(propCombox.getValue().getEntity().getId());
			if(fe.getFieldEnum().equals(EditableFieldEnum.FIRST_NAME)){
				if(cb11.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.LAST_NAME)){
				if(cb12.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.TITLE)){
				if(cb13.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.GENDER)){
				if(cb14.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.NATIONALITY)){
				if(cb15.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			
			if(fe.getFieldEnum().equals(EditableFieldEnum.NO_OF_GUEST)){
				if(cb21.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.SHARE_RESERVATION)){
				if(cb22.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.ARRIVAL_DATE)){
				if(cb23.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.DEPART_DATE)){
				if(cb24.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.ARRIVAL_TIME)){
				if(cb25.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			else if(fe.getFieldEnum().equals(EditableFieldEnum.DEPART_TIME)){
				if(cb26.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			else if(fe.getFieldEnum().equals(EditableFieldEnum.EXPRESS_CHECK_OUT)){
				if(cb27.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			
			if(fe.getFieldEnum().equals(EditableFieldEnum.ADDRESS)){
				if(cb30.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.EMAIL)){
				if(cb31.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.MOBILE)){
				if(cb32.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.CITY)){
				if(cb33.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.STATE)){
				if(cb34.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.COUNTRY)){
				if(cb35.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			else if(fe.getFieldEnum().equals(EditableFieldEnum.POSTAL)){
				if(cb36.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			else if(fe.getFieldEnum().equals(EditableFieldEnum.PHONE)){
				if(cb37.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			
			if(fe.getFieldEnum().equals(EditableFieldEnum.PAYMENT_COMMENT)){
				if(cb41.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}else if(fe.getFieldEnum().equals(EditableFieldEnum.PAYMENT_METHOD)){
				if(cb42.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			
			if(fe.getFieldEnum().equals(EditableFieldEnum.RC_REMARKS)){
				if(cb52.getValue()){
					fe.setEditable(true);
				}else{
					fe.setEditable(false);
				}
			}
			
		}
	}

	private void loadProperty() {
		propGroupIdNames = SessionContext.getInstance().getPropertyGroup();
		propListStore.removeAll();
		if (0 != propGroupIdNames.size()) {
			int propIndex = 0;
			for (IdNameModel<Property> inm : propGroupIdNames) {
				propListStore.add(new PropertyIdNameModelData(inm));
				if (propIndex == 0) {
					if (SessionContext.getInstance().getPropertyId() == inm.getId()) {
						propIndex = propListStore.getCount() - 1;
					}
				}
			}
			propCombox.setValue(propListStore.getAt(propIndex));
		} else {
			IdNameModel<Property> tempINM = new IdNameModel<Property>(SessionContext.getInstance().getPropertyId(), SessionContext.getInstance().getPropertyName());
			propListStore.add(new PropertyIdNameModelData(tempINM));
			propCombox.setValue(propListStore.getAt(0));
		}
//		propListStore.removeAll();
//		IdNameModel<Property> tempINM = new IdNameModel<Property>(SessionContext.getInstance().getPropertyId(), SessionContext.getInstance().getPropertyName());
//		propListStore.add(new PropertyIdNameModelData(tempINM));
//		propCombox.setValue(propListStore.getAt(0));
//		grid.setCurrentProperty(propListStore.getAt(0));
	}
}
